package com.mine;

public class TwoFind {
    private static int twoFind(char[] array, char target) {
        if (array == null || array.length == 0) {
            return -1;
        }
        return helper(array, 0, array.length - 1, target);
    }

    private static int helper(char[] array, int left, int right, char target) {
        if (left > right) {
            return -1;
        }
        int mid = (left + right) / 2;
        if (target < array[mid]) {
            return helper(array, left, mid - 1, target);
        } else if (target > array[mid]) {
            return helper(array, mid + 1, right, target);
        } else {
            return mid;
        }
    }

    private static int twoFind2(char[] array, char target) {
        if (array == null || array.length == 0) {
            return -1;
        }
        int left = 0;
        int right = array.length - 1;
        while (left <= right) {
            int mid = (left + right) / 2;
            if (target > array[mid]) {
                left = mid + 1;
            } else if (target < array[mid]) {
                right = mid - 1;
            } else {
                return mid;
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        System.out.println(twoFind(new char[]{'a', 'b', 'c', 'd', 'e'}, 'd'));
        System.out.println(twoFind2(new char[]{'a', 'b', 'c', 'd', 'e'}, 'd'));
    }
}
